package com.example.home.metro.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.metro.activity.PathMessageDetail;
import com.example.home.metro.bean.PathMessageBean;
import com.google.gson.Gson;

import java.util.List;

public class PathMessageAdapter extends RecyclerView.Adapter<PathMessageAdapter.MyHoldy> {
    private Context context;
    private List<PathMessageBean.DataBean> list;

    public PathMessageAdapter(Context context, List<PathMessageBean.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public MyHoldy onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_path,null);
        return new PathMessageAdapter.MyHoldy(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHoldy holder, int position) {
        holder.lineName.setText(list.get(position).getLineName());
        holder.nextStep.setText("下一站："+list.get(position).getNextStep().getName());
        holder.reachTime.setText("到达本站时长："+list.get(position).getReachTime()+"分钟");
        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PathMessageDetail.class);
                Gson gson = new Gson();
                intent.putExtra("pathId",gson.toJson(list.get(position)));
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class MyHoldy extends RecyclerView.ViewHolder {
        private TextView lineName;
        private TextView nextStep;
        private TextView reachTime;
        private LinearLayout rootView;

        public MyHoldy(@NonNull View itemView) {
            super(itemView);
            lineName = itemView.findViewById(R.id.lineName);
            nextStep = itemView.findViewById(R.id.nextStep);
            reachTime = itemView.findViewById(R.id.reachTime);
            rootView = itemView.findViewById(R.id.rootView);
        }
    }
}
